package com.taiji.dianthus.controller.sys;


import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.sys.DSysMenu;
import com.taiji.dianthus.domain.sys.DSysUser;
import com.taiji.dianthus.service.sys.MenuService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.util.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.taiji.dianthus.common.Constant.*;

/**
 * @ClassName SysMenuViewController
 * @Description 系统菜单相关
 * @Author H.M
 * @Date 2020/3/17
 */
@RequestMapping(value = "/sys/menu")
@Controller
public class SysMenuViewController extends BasicController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private SysLogService sysLogService;

    /**
     * 获取主菜单列表
     *
     * @param model
     * @param pageSize
     * @param currentPage
     * @return
     */
    @RequestMapping(value = "/list")
    public String showMenuList(HttpServletRequest request, Model model, Integer pageSize, Integer currentPage) {
        DSysUser currentUser = currentUser();
        pageSize = getPageSize(pageSize);
        currentPage = getCurrentPage(currentPage);
        BusinessMessage message = menuService.listRootMenus(pageSize, currentPage);
        sysLogService.save(currentUser, LOG_OPERATION_QUERY, IPUtil.getIpAddr(request),
                "查询菜单列表", message.getSuccess() ? 1 : 0, message.getMsg());
        model.addAttribute("menu", message.getData());
        model.addAttribute("currentUser", currentUser);
        model.addAttribute("menus", listMenu());
        return "/pages/sys/menuList";
    }

    /**
     * 跳转新增/编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/add")
    public String addMenu(Model model, String id) {
        DSysMenu menu = new DSysMenu();
        if (!StringUtils.isEmpty(id)) {
            menu = menuService.getByMenuId(id);
        }
        model.addAttribute("menu", menu);
        model.addAttribute("currentUser", currentUser());
        model.addAttribute("menus", listMenu());
        return "/pages/sys/menuAdd";
    }

    /**
     * 跳转子菜单
     *
     * @param model
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/subMenu")
    public String showSubMenu(Model model, String menuId) {
        List<DSysMenu> menus = menuService.listChildMenu(menuId);
        DSysMenu parentMenu = menuService.getByMenuId(menuId);
        model.addAttribute("menuList", menus);
        model.addAttribute("parentMenu", parentMenu);
        model.addAttribute("currentUser", currentUser());
        model.addAttribute("menus", listMenu());
        return "/pages/sys/subMenuList";
    }

    /**
     * 跳转新增/编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/addSubMenu")
    public String addSubMenu(Model model, String id, String parentId) {
        DSysMenu menu = new DSysMenu();
        if (!StringUtils.isEmpty(id)) {
            //编辑
            menu = menuService.getByMenuId(id);
            parentId = menu.getParentId();
        }
        DSysMenu parentMenu = menuService.getByMenuId(parentId);
        model.addAttribute("submenu", menu);
        model.addAttribute("menu", parentMenu);
        model.addAttribute("currentUser", currentUser());
        model.addAttribute("menus", listMenu());
        return "/pages/sys/subMenuAdd";
    }

    @RequestMapping(value = "/save")
    public String saveMenu(HttpServletRequest request, Model model, DSysMenu menu) {
        BusinessMessage message = menuService.saveMenu(menu);
        DSysUser currentUser = currentUser();
        if (StringUtils.isEmpty(menu.getId())) {
            sysLogService.save(currentUser, LOG_OPERATION_SAVE, IPUtil.getIpAddr(request),
                    "新增系统菜单[" + menu.getMenuName() + "]", message.getSuccess() ? 1 : 0, message.getMsg());
        } else {
            sysLogService.save(currentUser, LOG_OPERATION_UPDATE, IPUtil.getIpAddr(request),
                    "更新系统菜单[" + menu.getMenuName() + "]", message.getSuccess() ? 1 : 0, message.getMsg());
        }

        return "redirect:/sys/menu/list";
    }

    @RequestMapping(value = "/subSave")
    public String saveSubMenu(HttpServletRequest request, Model model, DSysMenu menu) {
        BusinessMessage message = menuService.saveMenu(menu);
        DSysUser currentUser = currentUser();
        if (StringUtils.isEmpty(menu.getId())) {
            sysLogService.save(currentUser, LOG_OPERATION_SAVE, IPUtil.getIpAddr(request),
                    "新增子菜单[" + menu.getMenuName() + "]", message.getSuccess() ? 1 : 0, message.getMsg());
        } else {
            sysLogService.save(currentUser, LOG_OPERATION_UPDATE, IPUtil.getIpAddr(request),
                    "更新子菜单[" + menu.getMenuName() + "]", message.getSuccess() ? 1 : 0, message.getMsg());
        }
        return "redirect:/sys/menu/subMenu?menuId=" + menu.getParentId();
    }

}
